﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using ThreeByte.ActiveDeck.Data.UserResponse;


namespace ThreeByte.ActiveDeck.Config
{
    public class QuestionConfig : PageConfig
    {

        #region Public Properties

        private string _question;
        public string Question {
            get {
                return _question;
            }
            set {
                _question = value;
                NotifyPropertyChanged("Question");
            }
        }

        private string _answer;
        public string Answer {
            get {
                return _answer;
            }
            set {
                _answer = value;
                NotifyPropertyChanged("Answer");
                NotifyPropertyChanged("HasAnswer");
            }
        }

        private string _correctAnswerOverride;
        public string CorrectAnswerOverride {
            get {
                return _correctAnswerOverride;
            }
            set {
                _correctAnswerOverride = value;
                NotifyPropertyChanged("CorrectAnswerOverride");
            }
        }

        private string _incorrectAnswerOverride;
        public string IncorrectAnswerOverride {
            get {
                return _incorrectAnswerOverride;
            }
            set {
                _incorrectAnswerOverride = value;
                NotifyPropertyChanged("IncorrectAnswerOverride");
            }
        }

        private bool _instantFeedback;
        public bool InstantFeedback {
            get {
                return _instantFeedback;
            }
            set {
                _instantFeedback = value;
                NotifyPropertyChanged("InstantFeedback");
            }
        }

        public bool HasAnswer {
            get {
                //If there is no answer, provide no feedback
                return !string.IsNullOrWhiteSpace(Answer);
            }
        }

        #endregion Public Properties

        public QuestionConfig() {
            LayoutOrientation = Content.Orientation.Portrait; //By Default
        }

        public QuestionConfig(XElement node, Dictionary<Guid, GraphicResource> resources)
            : base(node, resources) {
            Question = Uri.UnescapeDataString(node.Element(ns + "Question").Attribute("Text").Value);

            if(node.Element(ns + "Answer") != null) {
                Answer = node.Element(ns + "Answer").Attribute("Text").Value;
            }
            if(node.Attribute("InstantFeedback") != null) {
                InstantFeedback = bool.Parse(node.Attribute("InstantFeedback").Value);
            }

            if(node.Element(ns + "CorrectAnswer") != null) {
                CorrectAnswerOverride = Uri.UnescapeDataString(node.Element(ns + "CorrectAnswer").Attribute("Text").Value);
            }
            if(node.Element(ns + "IncorrectAnswer") != null) {
                IncorrectAnswerOverride = Uri.UnescapeDataString(node.Element(ns + "IncorrectAnswer").Attribute("Text").Value);
            }
        }

        public override XElement ToXml() {
            XElement node = base.ToXml();
            node.Add(new XElement(ns + "Question", new XAttribute("Text", Uri.EscapeDataString(Question ?? string.Empty))));

            if(!(string.IsNullOrEmpty(Answer))){
                node.Add(new XElement(ns + "Answer", new XAttribute("Text", Answer)));
            }
            if(InstantFeedback) {
                node.Add(new XAttribute("InstantFeedback", InstantFeedback));
            }
            if(!string.IsNullOrWhiteSpace(CorrectAnswerOverride)) {
                node.Add(new XElement(ns + "CorrectAnswer", new XAttribute("Text",  Uri.EscapeDataString(CorrectAnswerOverride))));
            }
            if(!string.IsNullOrWhiteSpace(IncorrectAnswerOverride)) {
                node.Add(new XElement(ns + "IncorrectAnswer", new XAttribute("Text",  Uri.EscapeDataString(IncorrectAnswerOverride))));
            }
            return node;
        }

        public ShortAnswerUserData InitUserData() {
            ShortAnswerUserData userData = new ShortAnswerUserData(1);
            return userData;
        }

        public override XElement GetReportContext(AggregateUserData userData) {
            XElement data = userData.ToXml();
            data.Add(new XAttribute("Question", Question));
            return data;
        }

        public XElement GetGradingConfig() {
            XElement gradeNode = new XElement(ns + "Grading");

            if(HasAnswer && InstantFeedback) {
                gradeNode.Add(new XAttribute("InstantFeedback", InstantFeedback));
            }

            string correctAnswerFeedback = CorrectAnswerOverride;
            if(string.IsNullOrWhiteSpace(correctAnswerFeedback)) {
                correctAnswerFeedback = GetDefaultCorrectText();
            }
            gradeNode.Add(new XAttribute("CorrectAnswerFeedback", Uri.EscapeDataString(correctAnswerFeedback)));

            string incorrectAnswerFeedback = IncorrectAnswerOverride;
            if(string.IsNullOrWhiteSpace(incorrectAnswerFeedback)) {
                incorrectAnswerFeedback = GetDefaultIncorrectText();
            }
            gradeNode.Add(new XAttribute("IncorrectAnswerFeedback", Uri.EscapeDataString(incorrectAnswerFeedback)));

            gradeNode.Add(new XElement(ns + "Question", new XAttribute("Text", Question)));
            if(!string.IsNullOrWhiteSpace(Answer)) {
                gradeNode.Add(new XElement(ns + "CorrectAnswer", new XAttribute("Text", Answer)));
            }

            return gradeNode;
        }

        internal string GetDefaultCorrectText() {
            return "That's Correct!";
        }

        internal string GetDefaultIncorrectText() {
            if(string.IsNullOrWhiteSpace(Answer)) {
                return "There is no correct answer";
            } else {
                return string.Format("Sorry, the correct answer is \"{0}\"", Answer);
            }

        }

        
    }
}
